#include <REGX52.H>
#include "MatrixKey.h"
#include "LCD1602.h"

unsigned char KeyNum;
unsigned int Password;
unsigned char count;

void main()
{
	LCD_Init();
	LCD_ShowString(1,1,"Password:");

	while(1)
	{
		KeyNum=MatrixKey();
		if(KeyNum)//确保下一次按下，LCD1602才更新显示
		{
			//输入密码
			if(KeyNum<=10)
			{
				if(count<4)//0~3,共4位密码
				{
					Password*=10;
					Password+=KeyNum%10;//1~9和0
					count++;				
				}
				LCD_ShowNum(2,1,Password,4);//更新显示，共16列
			}
			
			//确认密码
			if(KeyNum==11)
			{
				if(Password==2345)
				{
					LCD_ShowString(1,14,"OK " );
					Password=0;
					count=0;
					LCD_ShowNum(2,1,Password,4);
				}
				else
				{
					LCD_ShowString(1,14,"ERR" );
					Password=0;
					count=0;
					LCD_ShowNum(2,1,Password,4);
				}
			}
			
			//清空
			if(KeyNum==12)
			{
				Password=0;
				count=0;
				LCD_ShowNum(2,1,Password,4);
			}
			
			//一次删除一格
			if(KeyNum==13)
			{
				Password/=10;
				count--;
				LCD_ShowNum(2,1,Password,4);
			}
		}
	}
}